package com.longdaji.decoration.ui.register;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.ui.register.graduation.GraduationActivity;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class RegisterActivity extends BaseActivity implements RegisterContract.IView {

    @Inject
    RegisterContract.IPresenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.et_telephone)
    EditText etTelephone;
    @BindView(R.id.et_verification_code)
    EditText etVerificationCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_get_verification_code)
    Button btnGetVerificationCode;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.iv_hide_password)
    ImageView ivHidePassword;

    private long totalTime = 60;
    private Disposable disposable;
    private boolean isPasswordHidden = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setUnBinder(ButterKnife.bind(this));

        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("手机号注册");

        addTextWatcher();

    }

    private void addTextWatcher() {
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() >= 6 && editable.length() <= 16) {
                    btnConfirm.setEnabled(true);
                } else {
                    btnConfirm.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        if (disposable != null && !disposable.isDisposed())
            disposable.dispose();
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back, R.id.btn_confirm, R.id.btn_get_verification_code, R.id.iv_hide_password})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            case R.id.btn_confirm:
                mPresenter.onConfirmClick(etTelephone.getText().toString(), etPassword.getText().toString(), etVerificationCode.getText().toString());
                break;
            case R.id.btn_get_verification_code:
                mPresenter.onGetVerificationCodeClick(etTelephone.getText().toString());
                break;
            case R.id.iv_hide_password:
                mPresenter.onHidePasswordClick(isPasswordHidden);
                break;
            default:
                break;
        }
    }

    @Override
    public void calculateTime() {
        btnGetVerificationCode.setEnabled(false);
        Observable.interval(1000, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        if (totalTime == 1) {
                            disposable.dispose();
                            btnGetVerificationCode.setText("重新发送");
                            btnGetVerificationCode.setEnabled(true);
                            totalTime = 60;
                            return;
                        }
                        totalTime -= 1;
                        btnGetVerificationCode.setText(totalTime + "s");

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void showTelephoneVerifyFailed() {
        Toast.makeText(this, "当前手机号已被注册", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showSendVerification() {
        Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showStepTwo() {
        Intent intent = new Intent(this, GraduationActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("telephone", etTelephone.getText().toString());
        bundle.putString("password", etPassword.getText().toString());
//        bundle.putString("code", etVerificationCode.getText().toString());
        bundle.putBoolean("isRegister", true);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    @Override
    public void showIllegalTelephone() {
        Toast.makeText(this, "不合法的手机号码", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showHidePassword(boolean isHidePassword) {
        if (isHidePassword) {
            isPasswordHidden = true;
            ivHidePassword.setImageResource(R.mipmap.hide_password);
            etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            etPassword.setSelection(etPassword.length());
        } else {
            isPasswordHidden = false;
            ivHidePassword.setImageResource(R.mipmap.display_password);
            etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            etPassword.setSelection(etPassword.length());
        }
    }

    @Override
    public void showCodeError() {
        Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();
    }
}
